package com.halfsword.creativecraft.item.custom;

import com.halfsword.creativecraft.CreativeCraft;
import com.halfsword.creativecraft.item.ItemLoader;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.ArmorMaterial;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient;
import java.util.function.Supplier;

/**
 * 自定义盔甲材质枚举
 * 定义了模组中所有自定义盔甲的属性
 */
public enum ArmorMaterials implements ArmorMaterial {
    /**
     * 微量 空间盔甲材质
     * 属性：高耐久、高防护、高韧性
     */
    SPACE(
            "space",           // 材质名称
            450,               // 耐久倍率
            new int[] {10, 14, 12, 10}, // 护甲值 [靴子, 护腿, 胸甲, 头盔]
            75,                // 附魔能力
            SoundEvents.ARMOR_EQUIP_NETHERITE, // 装备音效
            6f,                // 韧性
            0.5f,                // 击退抗性
            () -> Ingredient.of(ItemLoader.GEMSPACE.get()) // 修复材料
    ),

    /**
     * 末影盔甲材质
     * 属性：中等耐久、中等防护
     */
    ENDER(
            "ender",          // 材质名称
            250,               // 耐久倍率
            new int[] {6, 7, 7, 6}, // 护甲值 [靴子, 护腿, 胸甲, 头盔]
            45,                // 附魔能力
            SoundEvents.ARMOR_EQUIP_IRON, // 装备音效
            4f,                // 韧性
            0f,                // 击退抗性
            () -> Ingredient.of(Items.ENDER_PEARL) // 修复材料
    );

    // 基础耐久值（按盔甲类型：靴子、护腿、胸甲、头盔）
    private static final int[] BASE_DURABILITY = {11, 16, 16, 13};

    private final String name;                  // 材质名称（不含命名空间）
    private final int durabilityMultiplier;     // 耐久倍率
    private final int[] protectionAmounts;      // 各部位护甲值
    private final int enchantmentValue;         // 附魔能力
    private final SoundEvent equipSound;        // 装备音效
    private final float toughness;              // 韧性值
    private final float knockbackResistance;    // 击退抗性
    private final Supplier<Ingredient> repairIngredient; // 修复材料供应器

    /**
     * 盔甲材质构造函数
     *
     * @param name                材质名称
     * @param durabilityMultiplier 耐久倍率
     * @param protectionAmounts    各部位护甲值数组 [靴子, 护腿, 胸甲, 头盔]
     * @param enchantmentValue     附魔能力
     * @param equipSound           装备音效
     * @param toughness            韧性值
     * @param knockbackResistance  击退抗性
     * @param repairIngredient     修复材料供应器
     */
    ArmorMaterials(
            String name,
            int durabilityMultiplier,
            int[] protectionAmounts,
            int enchantmentValue,
            SoundEvent equipSound,
            float toughness,
            float knockbackResistance,
            Supplier<Ingredient> repairIngredient
    ) {
        // 验证护甲值数组长度
        if (protectionAmounts.length != 4) {
            throw new IllegalArgumentException("护甲值数组必须包含4个元素 [靴子, 护腿, 胸甲, 头盔]");
        }

        this.name = name;
        this.durabilityMultiplier = durabilityMultiplier;
        this.protectionAmounts = protectionAmounts;
        this.enchantmentValue = enchantmentValue;
        this.equipSound = equipSound;
        this.toughness = toughness;
        this.knockbackResistance = knockbackResistance;
        this.repairIngredient = repairIngredient;
    }

    @Override
    public int getDurabilityForType(ArmorItem.Type type) {
        // 根据盔甲类型计算耐久值
        return BASE_DURABILITY[type.ordinal()] * this.durabilityMultiplier;
    }

    @Override
    public int getDefenseForType(ArmorItem.Type type) {
        // 根据盔甲类型获取护甲值
        return this.protectionAmounts[type.ordinal()];
    }

    @Override
    public int getEnchantmentValue() {
        return enchantmentValue;
    }

    @Override
    public SoundEvent getEquipSound() {
        return this.equipSound;
    }

    @Override
    public Ingredient getRepairIngredient() {
        return this.repairIngredient.get();
    }

    @Override
    public String getName() {
        // 返回带命名空间的完整材质名称
        return CreativeCraft.MOD_ID + ":" + this.name;
    }

    @Override
    public float getToughness() {
        return this.toughness;
    }

    @Override
    public float getKnockbackResistance() {
        return this.knockbackResistance;
    }

    /**
     * 获取材质的本地化名称键
     * @return 本地化键名
     */
    public String getLocalizationKey() {
        return "armor_material." + CreativeCraft.MOD_ID + "." + this.name;
    }

    /**
     * 获取材质的套装效果提示键
     * @return 提示文本键名
     */
    public String getSetBonusKey() {
        return "tooltip.creativecraft." + this.name + "_set_bonus";
    }

    /**
     * 获取材质的套装效果描述键
     * @return 描述文本键名
     */
    public String getSetDescriptionKey() {
        return "tooltip.creativecraft." + this.name + "_set_description";
    }
}
